<template>
  <div class="app-container">
      <el-form ref="companyForm" :rules="rules" :model="companyForm" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">

        <el-form-item label="简称" prop="companyName">
          <el-input v-model="companyForm.companyName" />
        </el-form-item>
        <el-form-item label="全称" prop="fullname">
          <el-input v-model="companyForm.fullname" />
        </el-form-item>
      <el-form-item label="封面图片">
        <Upload v-model="companyForm.logo" />
      </el-form-item>
        <el-form-item label="法人" prop="breed">
          <el-radio-group v-model="companyForm.breed">
            <el-radio :label="1">集团</el-radio>
            <el-radio :label="2">控股平台</el-radio>
            <el-radio :label="3">项目</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="注册日期" prop="establishDate">
          <el-date-picker 
            v-model="companyForm.establishDate"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="法人" prop="corporation">
          <el-input v-model="companyForm.corporation" />
        </el-form-item>
        <el-form-item label="职能" prop="positionID">
          <el-input v-model="companyForm.positionID" />
        </el-form-item>
        <el-form-item label="区域" prop="districtID">
          <el-input v-model="companyForm.districtID" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="companyForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
        </el-form-item>
      </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="saveCompany()">
        确认
      </el-button>
      <el-button v-if="companyForm.status.value === 5" type="danger" @click="handleDelete(companyForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import {  saveCompany, getCompanyDetail, removeCompanyByID } from '@/api/mechanism/company'


import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'

import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Upload },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      companyForm: {
        ID: undefined,
        remark: '',
        company_ID: 0,
        title: '',
        status: {
          value:1
        }
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      }
    }
  },
  created() {
    let parentCompanyID = this.$route.query && this.$route.query.parentCompanyID
    if(parentCompanyID != undefined){
      this.companyForm.parentCompanyID = parentCompanyID
    }
    let companyID = this.$route.query && this.$route.query.companyID
    if (companyID !== undefined) {
      companyID = parseInt(companyID)
      if (!isNaN(companyID)) {
        this.listLoading = true
        getCompanyDetail({
          "companyID":companyID
        }).then(response => {
          if (response.status === 200) {
            this.companyForm = response.data
            console.log(this.companyForm)
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'company' })
    },
    saveCompany() {
      let _this = this
      this.$refs['companyForm'].validate((valid) => {
        if (valid) {
          try {
            _this.companyForm.status = _this.companyForm.status.value;
          } catch (error) {
            _this.companyForm.status = 1;
          }
          if(_this.companyForm.hasOwnProperty('ID')){
            if(_this.companyForm.ID !== undefined){
              _this.companyForm.companyID = _this.companyForm.ID;
            }

          }
          saveCompany(_this.companyForm).then((response) => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'company' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
          })
        }
      })
    },
    handleDelete(row, index) {
      removeCompanyByID(row.ID).then(() => {
          if (response.status === 200) {
            this.$message({
              message: '操作Success',
              type: 'success'
            })
            this.$router.push({ path: 'company' })
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    }
  }
}
</script>
